package com.dc.refactoring._6_处理概括关系._10_FormTemplateMethod.step1;

import com.dc.refactoring._0_入门.Rental;
import com.google.common.collect.Lists;
import lombok.Data;
import lombok.experimental.Accessors;

import java.util.List;

/**
 * Description: 当前引用 _0_入门 的案例
 * @author: gdc
 * @date: 2020/12/13
 * @version 1.0
 */
@Data
@Accessors(chain = true)
public class Customer {

    /**
     * 客户名称
     */
    private String name;
    /**
     * 租赁信息列表
     */
    private List<Rental> rentals = Lists.newArrayList();

    /**
     * 添加租赁信息
     *
     * @param rental 租赁信息
     */
    public void addRental(Rental rental) {
        rentals.add(rental);
    }



    // step 4 将搬移的函数，引用指向 “新建子类” 的对应实现

    /**
     * 普通打印会话
     */
    /*public String value() {
        String result = getName() + "的租赁记录信息为：\n";
        for (Rental rental : rentals) {
            // 展示电影明细
            result += "\t" + rental.getMovie().getTitle() + "\t" + rental.getCharge() + "\n";
        }

        result += "总金额为：" + getTotalCharge() + "\n";
        result += "你获得的积分为：" + getTotalFrequentRenterPoints() + "\n";
        return result;
    }*/

    public String statement() {
        return new TextStatement().value(this);
    }

    /**
     * HTML 打印会话
     */
    /*public String value() {
        String result = "<h1>" + getName() + "的租赁记录信息为：</h1>";
        for (Rental rental : rentals) {
            // 展示电影明细
            result += "<p>" + rental.getMovie().getTitle() + "\t" + rental.getCharge() + "</p>";
        }

        result += "<p>总金额为：" + getTotalCharge() + "</p>";
        result += "<p>你获得的积分为：" + getTotalFrequentRenterPoints() + "</p>";
        return result;
    }*/

    public String htmlStatement() {
        return new HtmlStatement().value(this);
    }

    protected double getTotalCharge(){
        // 总金额
        double result = 0;
        for (Rental rental : rentals) {
            result += rental.getCharge();
        }
        return result;
    }

    protected int getTotalFrequentRenterPoints(){
        // 积分累计
        int result = 0;
        for (Rental rental : rentals) {
            // 计算积分
            result += rental.getFrequentRenterPoints();
        }
        return result;
    }

}